/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Mikael
 */
public class ReadWrite {

    /**
     * Object writing method
     *
     * @param x Object to be written.
     * @throws IOException
     */
    public final void writeObject(final Object x) throws IOException {
        try {
            FileOutputStream fileOut =
                    new FileOutputStream("score.ser");
            ObjectOutputStream out =
                    new ObjectOutputStream(fileOut);
            out.writeObject(x);
            out.close();
            fileOut.close();
        } catch (IOException i) {
            System.out.println("Could not write.");
        }
    }

    /**
     * Returns the object
     *
     * @return object
     * @throws IOException IO error
     * @throws ClassNotFoundException Byteclass error
     */
    public final Object readObject() throws IOException,
            ClassNotFoundException {
        try {
            FileInputStream fileIn =
                    new FileInputStream("score.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            HighScore x = (HighScore) in.readObject();
            in.close();
            fileIn.close();
            if (x == null) {
                return new HighScore();
            }
            return x;
        } catch (IOException i) {
            System.out.println("Could not read.");
        } catch (ClassNotFoundException c) {
            System.out.println("Class not found");
        }
        HighScore h = new HighScore();
        return h;
    }
}
